<template>
  <div class="app-container">
    <div class="top-container">
      <div class="title">信息总览</div>
      <div class="item-box">
        <div v-for="(item, index) in list" :key="index" class="top-item">
          <img :src="item.icon" alt="" />
          <div class="right">
            <div class="num">{{ item.data }}</div>
            <div class="text">{{ item.name }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="box-container">
      <div class="chart-box left">
        <div class="title-content">实施功率对比</div>
        <div class="item-content ">
          <chart
            v-if="chartOneData"
            :option="chartOneOption"
            style="width: 98%;"
          />
          <!-- <i class="el-icon-download" style="color: #fff;" /> -->
        </div>
      </div>
      <div class="chart-box right">
        <div class="title-content">环境对比</div>
        <div class="item-content">
          <chart
            v-if="chartThreeData"
            :option="chartThreeOption"
            style="width: 98%;"
          />
          <!-- <i class="el-icon-download" style="color: #fff;" /> -->
        </div>
      </div>
    </div>

    <div class="filter-container">
      <el-form v-model="listQuery" inline>
        <el-form-item label="">
          <el-select
            v-model="listQuery.category_id"
            size="small"
            default-first-option
            style="width: 180px"
            placeholder="系统类型"
            clearable
            @change="change"
          >
            <el-option
              v-for="(item, index) in joinArr"
              :key="index"
              :label="item.name"
              :value="item.category_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="listQuery.title"
            size="small"
            style="width: 200px"
            class="filter-item"
            placeholder="名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            查询
          </el-button>
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="edit"
            @click="showCreate"
          >
            添加
          </el-button>
          <!-- <el-button size="small" class="filter-item" type="primary">
            新建
          </el-button> -->
          <el-button size="small" class="filter-item" type="primary">
            批量添加
          </el-button>
          <el-button size="small" class="filter-item" type="primary">
            设备同步
          </el-button>
          <el-button size="small" class="filter-item" type="danger">
            批量删除
          </el-button>
          <el-button size="small" class="filter-item" type="primary">
            位置配置
          </el-button>
          <el-button size="small" class="filter-item" type="primary">
            设备类型配置
          </el-button>
        </el-form-item>
      </el-form>
      <!-- <div class="button-box">
        <el-button size="small" class="filter-item" type="primary">
          新建
        </el-button>
        <el-button size="small" class="filter-item" type="primary">
          批量添加
        </el-button>
        <el-button size="small" class="filter-item" type="primary">
          设备同步
        </el-button>
        <el-button size="small" class="filter-item" type="danger">
          批量删除
        </el-button>
        <el-button size="small" class="filter-item" type="primary">
          位置配置
        </el-button>
        <el-button size="small" class="filter-item" type="primary">
          设备类型配置
        </el-button>
      </div> -->
    </div>
    <div class="table-container">
      <el-table
        key="tableKey"
        v-loading="listLoading"
        :data="list"
        element-loading-text="加载中"
        border
        fit
        stripe
        highlight-current-row
        style="width: 100%"
        height="300"
        :header-cell-style="{
          background: '#E3ECFB',
          color: '#303133',
          fontWeight: '500'
        }"
      >
        <el-table-column align="center" label="编号" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="设备名称">
          <template slot-scope="scope">
            <span>/</span>
          </template>
        </el-table-column>
        <el-table-column label="设备信息" align="center">
          <el-table-column
            align="center"
            label="设备位置"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-image
                style="width: 60px;"
                :src="scope.row.image_path"
                :preview-src-list="[scope.row.image_path]"
              />
              <!-- <img style="width: 60px" :src="scope.row.image_path" /> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="设备状态">
            <template slot-scope="scope">
              <span>{{ scope.row.title }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="生产厂家">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="使用年限">
            <template slot-scope="scope">
              <!-- <el-tag :type="scope.row.status == 1 ? 'success' : 'warning'">
                <span>{{ scope.row.status == 1 ? '已显示' : '已停用' }}</span>
              </el-tag> -->
              <span>{{
                scope.row.status == null ? "无" : scope.row.status
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="维护人员">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="设备信息" align="center">
          <el-table-column align="center" label="参数一">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="参数二">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="参数三">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="参数四">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <!-- <el-table-column label="区域用电" align="center">
          <el-table-column align="center" label="公共区空调系统用电量" width="170">
            <template slot-scope="scope">
              <span>{{ scope.row.updated_at }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="公共区空调系统用电量" class-name="overflow-on" width="170">
            <template slot-scope="scope">
              {{ scope.row.sort }}
              <el-input v-if="scope.row.show == 1" v-model="scope.row.sort" size="small" max="50" />
            </template>
          </el-table-column>
        </el-table-column> -->

        <!-- <el-table-column align="center" label="操作" width="320">
          <template slot-scope="scope">
            <el-button v-if="scope.row.show == 1" plain size="small" @click="orderHandleDo(scope.row)">保存</el-button>
            <el-button v-else plain size="small" @click="orderHandle(scope.row)">排序</el-button>
            <el-button plain size="small" type="primary" @click="handleEdit(scope.row, 'edit')">编辑</el-button>
            <el-button v-if="scope.row.status == 1" plain size="small" type="warning"
              @click="changeStatus(scope.row, 0)">去停用</el-button>
            <el-button v-else plain size="small" type="success" @click="changeStatus(scope.row, 1)">去发布</el-button>
            <el-button plain size="small" type="danger" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <div class="pagination-container text-right">
        <el-pagination
          background
          :total="pagination.total"
          :current-page="listQuery.page"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form
        class="small-space"
        :model="temp"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="位置" required>
          <el-select
            v-model="temp.category_id"
            size="small"
            default-first-option
            style="width: 180px"
            placeholder="所属位置"
            clearable
          >
            <el-option
              v-for="(item, index) in joinArr"
              :key="index"
              :label="item.name"
              :value="item.category_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" required>
          <el-input v-model="temp.title" size="small" width="100px" />
        </el-form-item>

        <el-form-item label="跳转URL">
          <el-input
            v-model="temp.link_url"
            size="small"
            width="300"
            placeholder="例如：https://www.baidu.com"
          />
        </el-form-item>

        <el-form-item label="banner图" required>
          <el-upload
            class="avatar-uploader"
            :action="uploadPath"
            :headers="header"
            :show-file-list="false"
            :on-success="handleImageSuccess"
          >
            <img
              v-if="temp.image_path"
              :src="temp.image_path"
              style="width: 200px"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="temp.sort" size="small" />
          <el-tag type="warning">
            说明：正序排列，前台页面显示顺序由小到大显示</el-tag
          >
        </el-form-item>
        <el-alert
          :closable="false"
          title="温馨提示：图片尽量压缩后上传，尽量不超过1M"
          type="warning"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          type="primary"
          @click="create"
          >确 定</el-button
        >
        <el-button v-else type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDevices } from "@/api/equipment";
import { getToken } from "@/utils/auth";
import { uploadPath } from "@/utils/common";
import chart from "@/components/Echarts/chart";
const colors = [
  "#5470c6",
  "#91cc75",
  "#fac858",
  "#ee6666",
  "#73c0de",
  "#3ba272",
  "#fc8452",
  "#9a60b4",
  "#ea7ccc"
];

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  components: {
    chart
  },
  data() {
    return {
      list: null,
      joinArr: null,
      changeIndex: -1,
      pagination: {},
      dialogFormVisible: false,
      temp: {
        title: "",
        link_url: "",
        image_path: "",
        sort: 0
      },
      listQuery: {
        page: 1,
        limit: 10,
        title: "",
        category_id: null
      },
      uploadPath: uploadPath,
      dialogStatus: null,
      dialogTitle: "",
      header: {
        Authorization: getToken()
      },
      // 饼图数据
      pieData: [],
      chartOne: {},
      chartThree: {},
      chartOneOption: {},
      chartTwoOption: {},
      chartThreeOption: {},
      chartFourOption: {},
      chartOneData: false,
      chartTwoData: false,
      chartThreeData: false,
      chartFourData: false
    };
  },
  created() {
    this.getList();
    // this.getCtList(),
    this.getLineCount();

    this.getLineTwo();
    this.getBar();
  },

  methods: {
    getList() {
      this.listLoading = true;
      getDevices(this.listQuery).then(response => {
        var data = response.data;
        this.list = data.rates;
        const chartData = data.totals;
        this.pieData = [];
        // 图片相对路径处理
        chartData.forEach(item => {
          this.pieData.push({
            name: item.name,
            value: item.num
          });
        });
        this.getPieCount();
      });
      this.listLoading = false;
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },

    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    change(val) {
      this.$set(this.listQuery, "category_idC", null);

      List({ category_id: val }).then(response => {
        var data = response.data;
      });
    },
    showCreate() {
      this.temp = {
        category_id: null,
        title: "",
        link_url: "",
        image_path: "",
        sort: 0
      };
      this.dialogTitle = "添加";
      this.dialogStatus = "create";
      (this.dialogTitle = "添加"), (this.dialogFormVisible = true);
    },
    handleImageSuccess(res, file) {
      if (res.code === 0) {
        this.temp.image_path = res.data.filepath;
        // 图片相对路径处理
        // this.temp.image_path=process.env.VUE_APP_BASE_PIC +  this.temp.image_path
        this.temp.image_path = this.temp.image_path;
        // uploadPath = uploadPath.this.temp.image_path;

        this.$message.success("上传成功！");
      } else {
        this.$message.error(res.msg);
      }
    },
    update() {
      Save(this.temp).then(response => {
        if (response.code === 0) {
          this.$message({
            message: response.msg,
            type: "success"
          });
          this.dialogFormVisible = false;
          // this.fetchSetting()
          this.getList();
        } else {
          this.$message({
            message: response.msg,
            type: "error"
          });
        }
      });
    },
    // 绘制图表1
    getLineCount() {
      var SCCounts = {
        // title: {
        //   text: '本月访问趋势',
        //   x: 'center'
        //   // y: '35%',
        //   // itemGap: 2, // 副标题和主标题之间的距离，默认是10
        // },
        // color: colorList,
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c}"
        },
        legend: {
          data: ["污水厂_01", "污水厂02"],
          textStyle: {
            color: "#fff"
          }
        },
        textStyle: {
          color: "#fff"
        },
        grid: {
          left: 50,
          right: 50,
          top: 30,
          bottom: 30,
          borderWidth: 1
        },
        xAxis: {
          name: "(日期)",
          type: "category",
          boundaryGap: true,
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
        },
        yAxis: {
          name: "单位：kW",
          type: "value",
          nameTextStyle: {
            color: "#fff"
          }
        },
        series: [
          {
            name: "污水厂_01",
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: "line",
            smooth: true,
            symbolSize: 8
            // itemStyle: {
            //   color: 'rgb(140,205,130)'
            // }
          },
          {
            name: "污水厂02",
            data: [1820, 1932, 1901, 1934, 12290, 12330, 12320],
            type: "line",
            smooth: true,
            symbolSize: 8
            // itemStyle: {
            //   color: 'rgb(140,205,130)'
            // }
          }
        ]
      };

      this.chartThreeOption = SCCounts;
      this.chartThreeData = true;

      // var myChart = this.$echarts.init(this.$refs.SUMEchart)
      // SCCounts && myChart.setOption(SCCounts)
    },
    // 绘制饼图
    getPieCount() {
      var SCCounts = {
        // title: {
        //   text: '本月访问趋势',
        //   x: 'center'
        //   // y: '35%',
        //   // itemGap: 2, // 副标题和主标题之间的距离，默认是10
        // },
        // color: colorList,
        tooltip: {
          trigger: "item"
        },
        legend: {
          // right: 'center',
          orient: "vertical",
          right: 10,
          top: 20,
          bottom: 20,
          textStyle: {
            color: "#fff"
          }
        },
        series: [
          {
            name: "能耗",
            type: "pie",
            center: ["30%", "50%"],
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            label: {
              show: true,
              // position: 'center',
              color: "#fff"
            },
            labelLine: {
              show: true
            },
            data: this.pieData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };
      this.chartOneOption = SCCounts;
      this.chartOneData = true;
    },
    // 绘制对比图
    getLineTwo() {
      var SCCounts = {
        color: colors,
        tooltip: {
          trigger: "none",
          axisPointer: {
            type: "cross"
          }
        },
        legend: {
          textStyle: {
            color: "#fff"
          }
        },
        grid: {
          top: 70,
          bottom: 50,
          center: ["10%", "80%"],
          radius: ["40%", "70%"]
        },
        textStyle: {
          color: "#fff"
        },
        xAxis: [
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[1]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            yAxisIndex: 1,
            // prettier-ignore
            data: ['2016-1', '2016-2', '2016-3', '2016-4', '2016-5', '2016-6', '2016-7', '2016-8', '2016-9', '2016-10', '2016-11', '2016-12']
          },
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[0]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            yAxisIndex: 2,
            // prettier-ignore
            data: ['2015-1', '2015-2', '2015-3', '2015-4', '2015-5', '2015-6', '2015-7', '2015-8', '2015-9', '2015-10', '2015-11', '2015-12']
          }
        ],
        yAxis: [
          {
            type: "value",
            axisLine: {
              show: true
            }
          },
          {
            type: "value",
            position: "right",
            alignTicks: true,
            axisLine: {
              show: true
            }
          }
        ],
        series: [
          {
            name: "Precipitation(2015)",
            type: "line",
            xAxisIndex: 1,
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              2.6,
              5.9,
              9.0,
              26.4,
              28.7,
              70.7,
              175.6,
              182.2,
              48.7,
              18.8,
              6.0,
              2.3
            ]
          },
          {
            name: "Precipitation(2016)",
            type: "line",
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              3.9,
              5.9,
              11.1,
              18.7,
              48.3,
              69.2,
              231.6,
              46.6,
              55.4,
              18.4,
              10.3,
              0.7
            ]
          }
        ]
      };
      this.chartThreeOption = SCCounts;
      this.chartThreeData = true;
    },
    getBar() {
      var SCCounts = {
        color: colors,
        tooltip: {
          trigger: "none",
          axisPointer: {
            type: "cross"
          }
        },
        legend: {
          textStyle: {
            color: "#fff"
          }
        },
        textStyle: {
          color: "#fff"
        },
        grid: {
          top: 70,
          bottom: 50
          // center: ['30%', '50%'],
          // radius: ['40%', '70%'],
        },
        xAxis: [
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[1]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            // prettier-ignore
            data: ['2016-1', '2016-2', '2016-3', '2016-4', '2016-5', '2016-6', '2016-7', '2016-8', '2016-9', '2016-10', '2016-11', '2016-12']
          },
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[0]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            // prettier-ignore
            data: ['2015-1', '2015-2', '2015-3', '2015-4', '2015-5', '2015-6', '2015-7', '2015-8', '2015-9', '2015-10', '2015-11', '2015-12']
          }
        ],
        yAxis: [
          {
            type: "value"
          }
        ],
        series: [
          {
            name: "Precipitation(2015)",
            type: "bar",
            xAxisIndex: 1,
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              2.6,
              5.9,
              9.0,
              26.4,
              28.7,
              70.7,
              175.6,
              182.2,
              48.7,
              18.8,
              6.0,
              2.3
            ]
          },
          {
            name: "Precipitation(2016)",
            type: "bar",
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              3.9,
              5.9,
              11.1,
              18.7,
              48.3,
              69.2,
              231.6,
              46.6,
              55.4,
              18.4,
              10.3,
              0.7
            ]
          }
        ]
      };
      this.chartFourOption = SCCounts;
      this.chartFourData = true;
    }
  }
};
</script>

<style lang="scss" scoped>
.alert-container {
  margin: 20px 0;
}

.top-container {
  width: 100%;
  height: 139px;
  background-color: #fff;
  padding: 20px;

  .title {
    color: #000;
    margin-bottom: 12px;
  }

  .item-box {
    height: 80%;
    width: 100%;
    display: flex;
    justify-content: space-between;

    .top-item {
      width: 18%;
      height: 100%;
      background-color: #dadada99;

      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      display: flex;
      align-items: center;

      img {
        width: 60px;
        height: 60px;
        margin: auto 20px;
      }

      .right {
        color: #fff;
        width: 60%;

        .num {
          font-size: 28px;
        }
      }
    }
  }
}

.button-box {
  margin-bottom: 10px;
}

.box-container {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  margin: 10px 0;
  justify-content: space-between;

  .chart-box {
    width: 49.7%;
    height: 350px;
    background-color: #fff;
    margin-bottom: 10px;

    .title-content {
      display: flex;
      justify-content: space-between;
      color: #000;
      height: 50px;
      line-height: 50px;
      padding: 0 10px;
      background: #dfdfdf;
      align-items: center;
    }

    .item-content {
      display: flex;
      flex-wrap: wrap;
      height: calc(100% - 50px);
      padding: 10px;
    }
  }

  .left {
    width: 40%;
  }

  .right {
    width: 59.5%;
  }

  .half {
    width: 24.7%;
  }
}
</style>
